package org.farng.mp3.object;

import org.farng.mp3.TagUtility;


/**
 * <p>
 * Title:
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2002
 * </p>
 * 
 * <p>
 * Company:
 * </p>
 *
 * @author Eric Farng
 * @version $Revision: 1.7 $
 */
public class ObjectStringFixedLength extends AbstractMP3Object {
    /** DOCUMENT ME! */
    int length = 0;

    /**
     * Creates a new ObjectStringFixedLength object.
     *
     * @param identifier DOCUMENT ME!
     * @param length DOCUMENT ME!
     *
     * @throws IllegalArgumentException DOCUMENT ME!
     */
    public ObjectStringFixedLength(String identifier, int length) {
        if (length < 0) {
            throw new IllegalArgumentException("length is less than zero: " + length);
        }

        this.identifier = identifier;
        this.length     = length;
    }

    public ObjectStringFixedLength(ObjectStringFixedLength copyObject) {
        super(copyObject);

        this.length = copyObject.length;
    }

    /**
     * DOCUMENT ME!
     *
     * @param size DOCUMENT ME!
     */
    public void setLength(int size) {
        this.length = size;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public int getLength() {
        return length;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public int getSize() {
        return length;
    }

    /**
     * DOCUMENT ME!
     *
     * @param obj DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public boolean equals(Object obj) {
        if ((obj instanceof ObjectStringFixedLength) == false) {
            return false;
        }

        ObjectStringFixedLength object = (ObjectStringFixedLength) obj;

        if (this.length != object.length) {
            return false;
        }

        return super.equals(obj);
    }

    /**
     * DOCUMENT ME!
     *
     * @param str DOCUMENT ME!
     * @param offset DOCUMENT ME!
     *
     * @throws NullPointerException DOCUMENT ME!
     * @throws IndexOutOfBoundsException DOCUMENT ME!
     */
    public void readString(String str, int offset) {
        if (str == null) {
            throw new NullPointerException("String is null");
        }

        if ((offset < 0) || (offset >= str.length())) {
            throw new IndexOutOfBoundsException("Offset to String is out of bounds: offset = " + offset + ", string.length()" + str.length());
        }

        this.value = str.substring(offset, length + offset);
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String toString() {
        return writeString();
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String writeString() {
        String str = "";

        if (value == null) {
            str = String.valueOf(new char[length]);
        } else {
            int vlength = ((String) value).length();

            if (vlength > this.length) {
                str = ((String) value).substring(0, this.length);
            } else if (vlength == this.length) {
                str = (String) value;
            } else {
                str = TagUtility.padString((String) value, this.length, ' ', false);
            }
        }

        return str;
    }
}